﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace RFPL
{
    public partial class CountryInfo : Form
    {
        public int countryId = 0;
        public CountryInfo(int _countryId)
        {
            InitializeComponent();
            countryId = _countryId;
        }

        private void save_Click(object sender, EventArgs e)
        {
            if (countryId == 0)
            {

                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[addCountry]";
                myCommand.Parameters.Add("@name", SqlDbType.NVarChar, name.Text.Length).Value = Convert.ToString(name.Text);

                if (flagNew.Image != null)
                {
                    byte[] fl = ImageUtils.ImageToByteArray(flagNew.Image);
                    myCommand.Parameters.Add("@flag", SqlDbType.VarBinary, fl.Length).Value = fl;
                }
                else
                {
                    myCommand.Parameters.Add("@flag", SqlDbType.VarBinary, 0).Value = DBNull.Value;
                }

                conn.Open();
                countryId = Convert.ToInt32(myCommand.ExecuteScalar());
                conn.Close();
            }
            else
            {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString = Global.connectionString;
                SqlCommand myCommand = conn.CreateCommand();
                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "[editCountry]";
                myCommand.Parameters.Add("@id", SqlDbType.Int).Value = countryId;
                myCommand.Parameters.Add("@name", SqlDbType.NVarChar, name.Text.Length).Value = Convert.ToString(name.Text);

                if (flagNew.Image != null)
                {
                    byte[] fl = ImageUtils.ImageToByteArray(flagNew.Image);
                    myCommand.Parameters.Add("@flag", SqlDbType.VarBinary, fl.Length).Value = fl;
                }
                else
                {
                    myCommand.Parameters.Add("@flag", SqlDbType.VarBinary, 0).Value = DBNull.Value;
                }

                conn.Open();
                myCommand.ExecuteScalar();
                conn.Close();
            }

            this.Close();
           
        }

        private void flagNew_Click(object sender, EventArgs e)
        {
            ImageUtils.setImageFromStream(flagNew);
        }

        private void name_TextChanged(object sender, EventArgs e)
        {
            if (name.Text.Length > 0)
            {
                save.Enabled = true;
            }
            else {
                save.Enabled = false;
            }
        }

        private void CountryInfo_Load(object sender, EventArgs e)
        {
            if (countryId != 0) {

                SqlDataReader dataReader = Global.getDataReader("select flag, name from country where id = " + countryId);

                if (dataReader.Read())
                {
                    if (! DBNull.Value.Equals(dataReader["flag"]))
                    {
                        flagNew.Image = ImageUtils.ByteArrayToImage((byte[])dataReader["flag"]);
                    }
                    name.Text = Convert.ToString(dataReader["name"]);
                }
                dataReader.Close();
                Global.closeConnection();

            
            }

        }
    }
}
